﻿/*
 *	@email 辅助填充
 *
 *  <input type="text" class="text" name="username" id="text_username" placeholder="常用邮箱" />
 *	emailhelper("text_username",true);   
 *  <param>[inputid]输入邮箱的input控件的id  [isshow]是否显示错误提示框 </param>
 *
 */


var targetinput_id = null;             //目标input框ID[#email]
var popupdiv_id = null;                //邮件后缀选择提示框div [email]
var popupdiv_class = null;             //邮件后缀选择提示框div class [emailclass]
var errordiv_class = null;             //错误div class [emailclass]
var isshowerrordiv = null;            //是否显示错误提示
var Is_All_login = false;             //登录表单完全正确

var isfirstclick = true;        //是否是第一次点击

function emailhelper(inputid, isshow) {
 
    var nowid = null;
    var totalid = null;
    var can1press = false;
    var emailafter = null;
    var emailbefor = null;
    var emailvar = new Array("@163.com", "@sohu.com", "@yahoo.com", "@qq.com", "@sina.com", "@gmail.com");

    isshowerrordiv = isshow;
    targetinput_id = inputid;
    errordiv_class = targetinput_id + "error";
    popupdiv_id = targetinput_id + "popup";
    popupdiv_class = popupdiv_id + "_class";
    //文本框获得焦点，插入Email提示层
    $("#" + targetinput_id).focus(function () {
        error(1);
        $("#" + popupdiv_id).remove();
        $(this).after("<div id='" + popupdiv_id + "' style='width:267px; margin-top:22px; height:auto; background:#fff;color:#6B6B6B; text-align:left; position:absolute; left:" + $(this).get(0).offsetLeft + "px; top:" + ($(this).get(0).offsetTop + $(this).height() + 2) + "px; border:1px solid #ccc;z-index:2222; '></div>");
        if ($("#" + popupdiv_id).html()) {
            $("#" + popupdiv_id).css("display", "block");
            $("." + popupdiv_class).css("width", $("#" + popupdiv_id).width());
            can1press = true;
        }
        else {
            $("#" + popupdiv_id).css("display", "none");
            can1press = false;
        }
    }).blur(function () {//文本框失去焦点时,验证格式是否正确了
        isemail();
    })
    //文本框输入文字时，显示Email提示层和常用Email
        .keyup(function () {
            var press = $("#" + targetinput_id).val();
//            alert(press);
            if (press != "" || press != null) {
                var emailtxt = "";
                
                totalid = emailvar.length;
                //div是自己的输入，第一个选择显示
                var emailmy = "<div class='" + popupdiv_class + "' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#777'>" + press + "</font></div>";
                //@存在

                if (press.indexOf("@") > 0) {
                    emailbefor = press.split("@")[0];
                    emailafter = "@" + press.split("@")[1];
                    for (var i = 0; i < emailvar.length; i++) {
                        var theemail = emailvar[i];
                        if (theemail.indexOf(emailafter) == 0) {
                            //div是编辑框出现@后的选择
                            emailtxt = emailtxt + "<div class='" + popupdiv_class + "' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#008'>" + emailbefor + "</font>" + emailvar[i] + "</div>"
                        }
                    }
                } else {
                    for (var o = 0; o < emailvar.length; o++) {
                        //div是所有显示的
                        emailtxt = emailtxt + "<div class='" + popupdiv_class + "' style='width:170px; color:#6B6B6B;overflow:hidden;'><font color='#777'>" + press + "</font>" + emailvar[o] + "</div>"
                    }
                }
                $("#" + popupdiv_id).html(emailmy + emailtxt);
                if ($("#" + popupdiv_id).html()) {
                    $("#" + popupdiv_id).css("display", "block");
                    $("." + popupdiv_class).css("width", $("#" + popupdiv_id).width());
                    can1press = true;
                }
                else {
                    $("#" + popupdiv_id).css("display", "none");
                    can1press = false;
                }
                beforepress = press;
            }
            if (press == "" || press == null) {
                $("#" + popupdiv_id).html("");
                $("#" + popupdiv_id).css("display", "none");
            }
        })
    //文本框失焦时删除层
    $(document).click(function () { //文本框失焦时删除层
        if (can1press) {
            $("#"+popupdiv_id).remove();
            can1press = false;
            if ($("#"+targetinput_id).focus()) {
                can1press = false;
            }
        }
    })
    //鼠标经过提示Email时，高亮该条Email
    $("." + popupdiv_class).live("mouseover", function () {
        $("." + popupdiv_class).css("background", "#FFF");
        $(this).css("background", "#CACACA");
        nowid = $(this).index();
    })
    //鼠标点击Email时，文本框内容替换成该条Email，并删除提示层
        .live("click", function () {
            var newhtml = $(this).html();
            newhtml = newhtml.replace(/<.*?>/g, "");
            $("#" + targetinput_id).val(newhtml);
            $("#" + popupdiv_id).remove();
            var mail = $("#" + targetinput_id).val();
        })
};
//email完全检查是否正确
function isemail() {
    var mail = $("#" + targetinput_id).val();
    if (mail != '') {
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
        if (!reg.test(mail)) {   //错误
            error(0);
            Is_All_login = false;
        }
        else {   //完全正确
            error(1);
            Is_All_login = true;
        }
    }
    else {   //input为空
        error(0);
        Is_All_login = false;
    }
}
//注册邮箱错误弹出框
function error(num) {
    if (isshowerrordiv) {
        $("#" + targetinput_id).after("<div class='" + errordiv_class + "' style='width:270px; margin-top:25px; height:40px;  background:url(../../Content/image/login/login.png); background-position:0px -120px ; position:absolute; left:" + $("#" + targetinput_id).get(0).offsetLeft + "px; top:" + ($("#" + targetinput_id).get(0).offsetTop + $("#" + targetinput_id).height() + 2) + "px; z-index:5px; margin-top:25px; '><label style='margin-left:45px;display:block;text-align:left; height:50px ;width:auto; font:12px 方正姚体;color:#999; padding-top:13px'>请填写正确的邮箱</label></div>");
        if (num == 0) {
            $("." + errordiv_class).show();
        }
        else if (num == 1) {
            $("." + errordiv_class).hide();
        } 
    }
}

